<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div class="cmt-box">
          <!-- 头像 -->
          <van-image fit="cover" round :src="item.aut_photo"></van-image>

          <div class="info-box">
            <!-- 名字 -->
            <div class="name">{{ item.aut_name }}</div>
            <!-- 内容 -->
            <div class="content">{{ item.content }}</div>
            <div class="bottom-box">
              <span class="time">{{ item.pubdate | relvTime }}</span>
              <van-button @click="doReply(item)" size="mini"
                >回复（{{ item.reply_count }}）</van-button
              >
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>

    <!-- 发表区域 -->
    <div class="send-box">
      <van-field
        @keyup.enter="send"
        v-model="msg"
        center
        clearable
        placeholder="发表你的观点"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- 回复区域 -->
    <!-- v-model用来控制显示和隐藏 -->
    <van-action-sheet v-model="replyShow" title="发表回复">
      <reply v-if="replyShow" :cmt="cmt"/>
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from '@/api'
// 导入回复组件
import reply from './reply'
export default {
  components: {
      reply
  },
  data() {
    return {
      cmt: {},
      list: [],
      loading: false,
      finished: false,
      offset: null,
      msg: '',
      replyShow: false
    }
  },
  methods: {
    async onLoad() {
      let res = await cmtListAPI({
        type: 'a',
        source: this.$route.query.id,
        offset: this.offset
      })
      // 把数据存起来
      this.list.push(...res.data.data.results)
      // 把下一页的开头id也存起来方便查下一页
      this.offset = res.data.data.last_id

      // 把本次加载状态改成false
      this.loading = false
      // 判断全部加载完
      if (res.data.data.end_id === res.data.data.last_id) {
        this.finished = true
      }
    },

    // 发送的点击事件
    async send() {
      // 非空判断
      if (this.msg === '') return this.$toast('请输入点评论内容')

      // 代表不为空，就要发请求
      let res = await sendCmtAPI({
        // 对文章评论传入文章id
        target: this.$route.query.id,
        content: this.msg
      })

      // 把发表成功后的评论加到数组的最前面，就不用刷新也能看到数据了
      this.list.unshift(res.data.data.new_obj)

      // 清空输入框
      this.msg = ''
      // 提示成功
      this.$toast.success('评论成功！')
    },

    // 回复的点击事件
    doReply (item) {

      // 1. 给子组件把被点的这行数据传递过去
      this.cmt = item
      // 2. 显示出回复列表
      this.replyShow = true
    }
  }
}
</script>

<style lang="less" scoped>
.cmt-box {
  display: flex;

  .van-image {
    width: 40px;
    height: 40px;
  }

  .info-box {
    margin-left: 10px;
    flex: 1;
    .name {
      color: #466b9d;
    }

    .bottom-box {
      .time {
        color: gray;
      }
      .van-button {
        vertical-align: middle;
      }
    }
  }
}

.send-box {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
</style>
